package com.sdzee.dao;


import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.*;

import model.Event;
import model.Notification;

@Stateless
public class NotificationDaoEJB {
	
	//Injection du Manager qui s'occupe de la connexion avec la BDD
		@PersistenceContext (unitName = "mydb_PU")
		private EntityManager em;
		
		public void add(String eventAbout,String messageAlert_Invitation, String userDestinatore){
			Notification notif = new Notification();
			notif.setEventAbout(eventAbout);
			notif.setMessageAlert_Invitation(messageAlert_Invitation);
			notif.setUserDestinatore(userDestinatore);
			
			em.persist(notif);
		}
		public List<Notification> findMyNotifications(String pseudo){
			List<Notification> list = new ArrayList<Notification>() ;
			Query q = em.createQuery("select n from Notification n where n.userDestinatore=:N");
			q.setParameter("N", pseudo);
			try{
				list = (List<Notification>) q.getResultList();
				return list;
			} catch(Exception e){throw new DAOException (e);}
		}
		public void move(int id){
			Query query = em.createQuery("delete from Notification n where n.idnotification=:p");
			query.setParameter("p", id).executeUpdate();
		}
		
		public int getIdNotificatioByEventAbout(String name, String dest){
			Query q = em.createQuery("select n.idnotification from Notification n where n.eventAbout=:N and n.userDestinatore=:M");
			q.setParameter("N", name);
			q.setParameter("M", dest);
			
			return  (int)q.getSingleResult();
		}
		
		

}
